package com.ruoyi.smart.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.smart.config.CouchDBConfig;
import com.ruoyi.smart.domain.SmartProcessData;
import com.ruoyi.smart.service.ICouchdbSmartProcessData;
import org.ektorp.CouchDbConnector;

import org.ektorp.ViewQuery;
import org.ektorp.ViewResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


import java.util.List;



@Component
public class CouchdbSmartProcessDataImpl implements ICouchdbSmartProcessData {

    @Autowired
    private CouchDBConfig couchDBConfig;
    private CouchDbConnector connector;

    /**
     * 连接数据库
     * @throws Exception
     */
    @Bean(name = "CouchDbConnectorTest2")
    public void getCouchDbConnector() throws Exception {
        connector=couchDBConfig.couchDbConnector("gdf");
    }

    /**
     * 新增和修改
     * @param smartProcessData
     */
    @Override
    public String saveSmartProcessData(SmartProcessData smartProcessData) {
//            try {
//                SmartProcessData smartProcessData1=getSmartProcessData(smartProcessData);
//                smartProcessData.setRevision(smartProcessData1.getRevision());
//                connector.update(smartProcessData);
//            }catch (Exception e){
//                connector.create(String.valueOf(smartProcessData.getId()), smartProcessData);
//            }
        getViews();
        return String.valueOf(smartProcessData.getId());
    }


    /**
     * 按id获取某个数据
     * @param smartProcessData
     * @return
     */
    @Override
    public SmartProcessData getSmartProcessData(SmartProcessData smartProcessData){
        return connector.get(SmartProcessData.class,smartProcessData.getId());
    }


    /**
     * 获取视图信息
     */
    private void getViews() {
        ViewQuery query = new ViewQuery().viewName("Id").designDocId("_design/mrView").limit(1)/**.includeDocs(true)*/;
        ViewResult result = connector.queryView(query);
        System.out.println("查出来了"+JSON.toJSONString(result.getRows()));

    }


}